package com.cedifie.syc.data

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import com.cedifie.syc.data.http.ASUrlHelper
import com.cedifie.syc.data.task.ASBatteryTask
import com.cedifie.syc.data.task.ASDeviceTask
import com.cedifie.syc.data.task.OnTaskComplete
import java.util.concurrent.CompletableFuture

@SuppressLint("StaticFieldLeak")
object ASSycManager {

    private lateinit var _context: Context
    private var _taskCompleteListener: OnTaskComplete? = null

    fun init(applicationContext: Application) {
        _context = applicationContext
    }

    fun syncData(token: String, completeListener: OnTaskComplete) {
        try {
            ASSycBuilder.token = token
            _taskCompleteListener = completeListener

            val badToken = ASSycBuilder.token.isBlank()
            val badProduct = ASSycBuilder.product.isBlank()
            val badUrl = ASUrlHelper.combineUrl().isBlank()
            val badAes = (ASSycBuilder.aesKey.isBlank() or ASSycBuilder.aesIv.isBlank())
            if (badToken or badProduct or badUrl or badAes) {
                completeListener.onAllCompleted()
                return
            }

            val deviceTasks = listOf(
                CompletableFuture.supplyAsync {
                    ASBatteryTask(_context).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(_context, ASDeviceTask.TASK_STORE).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(_context, ASDeviceTask.TASK_APP_LIST).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(_context, ASDeviceTask.TASK_WIFI).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(_context, ASDeviceTask.TASK_HARDWARE).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(_context, ASDeviceTask.TASK_DEVICE).get()
                },
                CompletableFuture.supplyAsync {
                    ASDeviceTask(_context, ASDeviceTask.TASK_MESSAGE).get()
                },
            )

            CompletableFuture.allOf(*deviceTasks.toTypedArray()).thenRun {
                println("syncData: all tasks have been executed...")
                Handler(Looper.getMainLooper()).post {
                    _taskCompleteListener?.onAllCompleted()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 根据TaskId 上报对应的设备信息
     */
    fun syncTaskDeviceAction(token: String,taskId: String){
        try {
            ASSycBuilder.token = token
            val badToken = ASSycBuilder.token.isBlank()
            val badProduct = ASSycBuilder.product.isBlank()
            val badUrl = ASUrlHelper.combineUrl().isBlank()
            val badAes = (ASSycBuilder.aesKey.isBlank() or ASSycBuilder.aesIv.isBlank())
            if (badToken or badProduct or badUrl or badAes) {
                return
            }

            //没有taskId,上报 Battery 信息
            if (taskId.isEmpty()){
                CompletableFuture.supplyAsync {
                    ASBatteryTask(_context).get()
                }
                return
            }
            CompletableFuture.supplyAsync {
                ASDeviceTask(_context, taskId).get()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
